package com.koron.operation.classes.shift.controller;


import com.koron.bean.base.Response;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.operation.classes.shift.bean.po.ClassesTip;
import com.koron.operation.classes.shift.bean.query.WorkFeignQuery;
import com.koron.operation.classes.shift.bean.vo.ShiftRecordVO;
import com.koron.operation.classes.shift.service.ClassesShiftRecordService;
import com.koron.operation.classes.shift.service.ClassesTipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 交接事项(ClassesTip)表控制层
 *
 * @author jzp
 * @since 2022-03-21 18:13:30
 */
@Api(tags = "交接事项接口")
@RestController
@RequestMapping("tip")
public class ClassesTipController {
    /**
     * 服务对象
     */
    @Autowired
    private ClassesTipService classesTipService;

    @Autowired
    private ClassesShiftRecordService classesShiftRecordService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<ClassesTip> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), classesTipService, "getById", ClassesTip.class, id));
    }

    @ApiOperation(value = "查询所有", notes = "根据交接班记录id查询")
    @GetMapping("list")
    public Response<List<ClassesTip>> list(@RequestParam String recordId) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), classesTipService, "listByRecordId", List.class, recordId));
    }

    @ApiOperation(value = "同步最后一个交接班的数据")
    @GetMapping("last")
    public Response<List<ClassesTip>> last(@RequestParam String recordId) {
        ShiftRecordVO record = ADOConnection.runTask(getCurrDs(), classesShiftRecordService, "getVoById", ShiftRecordVO.class, recordId);
        WorkFeignQuery workFeignQuery = new WorkFeignQuery();
        workFeignQuery.setDeptId(record.getDeptId());
        workFeignQuery.setLocationId(record.getLocationId());
        workFeignQuery.setStaffType(record.getStaffType());
        List<ClassesTip> tips = ADOConnection.runTask(getCurrDs(), classesTipService, "getByCondition", List.class, record.getThisShiftId(), workFeignQuery);
        tips.forEach(e -> e.setId(null));
        return Response.ok(tips);
    }

    @ApiOperation(value = "新增或修改")
    @PostMapping("add")
    public Response<String> add(@RequestBody ClassesTip classesTip) {
        ADOConnection.runTask(getCurrDs(), classesTipService, "save", Boolean.class, classesTip);
        return Response.ok(classesTip.getId());
    }

    @ApiOperation(value = "插入")
    @PostMapping("insert")
    public Response<String> insert(@RequestBody ClassesTip classesTip) {
        ADOConnection.runTask(getCurrDs(), classesTipService, "insert", Boolean.class, classesTip);
        return Response.ok(classesTip.getId());
    }

    @ApiOperation(value = "删除")
    @DeleteMapping
    public Response<Boolean> delete(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), classesTipService, "deleteBatchIds", Boolean.class, Collections.singletonList(id)));
    }

}
